import request from '@/utils/request'
import { MessageBox, Message } from 'element-ui'

const api = {
	isUseList: [
		{ value: 0, isUse: "禁用" },
		{ value: 1, isUse: "启用" } //禁用启用下拉框
	  ],
}


//查询层级菜单 一二三级菜单
api.getListPurviewLevel = data => {
	let p = request({
		url: `/base/itouchSysPurview/getListPurviewLevel`,
		method: 'get',
		params: data
	})
	p.then(res=>{
		if(res.data.list.length==0){
			Message.error({ message: '无下级菜单或下级菜单已清空' })
		}
		res.data.list.forEach(e => {
			if(e.isUse==0){
				e.color = "#f00"
				e.linkType = 'danger'
			} else {
				e.color = ""
				e.linkType = 'primary'
			}
			if(e.purviewUrl!='/'){
				e.clickable = true
			} else {
				e.clickable = false
			}
			e.label = `【${e.orderNumber}】 ${e.purviewName}<br>(${e.purviewUrl})`
		});
		return res
	});
	return p;
}

//查询菜单信息
api.getListPurview = data => {
	return request({
		url: `/base/itouchSysPurview/getListPurview`,
		method: 'get',
		params: data
	})
}

//查看菜单页面下的权限列表
api.getAuthorityByPurviewId = data => {
	let p =  request({
		url: `/base/itouchSysPurview/getAuthorityByPurviewId`,
		method: 'get',
		params: data
	})
	p.then(res=>{
		res.data.list.forEach(e=>{
			if(e.isUse == 0){
				e.cls = 'text-red'
				e.isUseStr = '禁用'
			} else {
				e.cls = ''
				e.isUseStr = '启用'
			}
		})
		return res;
	})

	return p;
}

//单查询菜单信息
api.getOneItouchSysPurview = data => {
	return request({
		url: `/base/itouchSysPurview/getOneItouchSysPurview`,
		method: 'get',
		params: data
	})
}

//启用禁用菜单信息
api.updateIsUse = data => {
	return request({
		url: `/base/itouchSysPurview/updateIsUse`,
		method: 'post',
		data
	})
}

//添加修改菜单
api.saveOrUpdatePurview = data => {
	return request({
		url: `/base/itouchSysPurview/saveOrUpdatePurview`,
		method: 'post',
		data
	})
}

//删除菜单
api.delPurviewById = data => {
	return request({
		url: `/base/itouchSysPurview/delPurviewById`,
		method: 'post',
		data
	})
}

//系统角色的查询权限对应的菜单集合
api.getListForRole = roleId => {
	let p =  request({
		url: `/base/itouchSysPurview/getListForRole/${roleId}`,
		method: 'get'
	})
	p.then(res=>{
		eachMenu(res.data.list)
		function eachMenu(list){
			list.forEach(e=>{
				let disableText=''
				if(e.isUse == 0){
					e.disabled = true
					e.color = "red"
					e.linkType = 'danger'
					disableText = ' 【禁用】'
				}else{
					e.color = "green"
					e.linkType = 'primary'
				}
				if(e.purviewType==1){
					e.purviewName = '【菜单】'+e.purviewName + disableText
				} else {
					e.purviewName = '【权限】'+e.purviewName + disableText
				}
				if(e.menus){
					eachMenu(e.menus)
				}	
			})
		}

		return res;
	});
	return p;
}


//绑定和解除角色和菜单的关系
api.bindRoleAndMenu = data => {
	return request({
		url: `/base/itouchSysPurview/bindRoleAndMenu`,
		method: 'get',
		params:data
	})
}

//系统菜单查询一级菜单权限
api.getOneMenuAuthority = data => {
	let p = request({
	  url: `/base/itouchSysPurview/getOneMenuAuthority`,
	  method: 'get',
	  params: data
	})
	p.then(res=>{
	  res.data.list.forEach(e=>{
		if(e.isUse == 0){
		  e.isUse = '禁用'
		}else{
		  e.isUse = '启用'
		}
	  })
	  return res;
	})
	return p;
  }

  //根据菜单查询角色
  api.getListRoleNameByMenuId = purviewId => {
	return request({
		url: `/base/itouchSysPurview/getListRoleNameByMenuId?purviewId=${purviewId}`,
		method: 'get'
	})
}


export default api